const mongoose = require('mongoose')

const schema = new mongoose.Schema({
    name: {type: String},
    // 因为以id为查询依据，顾使用mongodb特有的类型架构ObjectId
    // 并以ref挂载带分类模型本身，只在分类模型Category中查找
    parent: {type: mongoose.SchemaTypes.ObjectId, ref: 'Category'},
})



schema.virtual('children', {
    localField: '_id',
    foreignField: 'parent',
    justOne: false,
    ref: 'Category'
  })
  
  schema.virtual('newsList', {
    localField: '_id',
    foreignField: 'categories',
    justOne: false,
    ref: 'Article'
  })

module.exports = mongoose.model('Category', schema)